import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
//

//
public class Visualisation {
    //
    private JFrame frame;
    private UploadDialog uploadDialog = null;
    private String filename;
    private MyController controller;

    private JMenuBar menuBar;
    private JMenu fileMenu;
    private JButton openButton;
    private JMenuItem exitItem;
    private JMenu aboutMenu;
    private JMenuItem aboutItem;
    private JTextArea log;


    public JButton getUploadButton() {
        return uploadButton;
    }

    private JButton uploadButton;


    public Visualisation() {
        controller = new MyController(this);
    }

    public void setController(MyController controller) {
        this.controller = controller;
    }


    public void update() {
        log.repaint();

    }

    public void addComponents(Container contentPane) {
        contentPane.setLayout(new BorderLayout());
        JPanel centerPanel = new JPanel(new BorderLayout());
        log = new JTextArea("No Files.");
        log.setBackground(Color.LIGHT_GRAY);
        log.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(log);
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//
        uploadButton = new JButton("upload");
        uploadButton.setEnabled(false);
        JPanel uploadButtonPanel = new JPanel();
        this.menuBar = new JMenuBar();
        this.fileMenu = new JMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        openButton = new JButton("Open");
        exitItem = new JMenuItem("Exit");
        exitItem.setMnemonic(KeyEvent.VK_X);
        uploadButtonPanel.add(openButton);
        uploadButtonPanel.add(uploadButton);        
        fileMenu.add(exitItem);

        aboutMenu = new JMenu("About");
        aboutItem = new JMenuItem("About", KeyEvent.VK_A);
        aboutMenu.add(aboutItem);
        menuBar.add(fileMenu);
        menuBar.add(aboutMenu);
        contentPane.add(menuBar, BorderLayout.NORTH);
        centerPanel.add(uploadButtonPanel, BorderLayout.NORTH);

        centerPanel.add(scrollPane, BorderLayout.CENTER);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        openButton.addActionListener(controller.getOpenListener());
//        saveItem.addActionListener(controller.getSaveListener());
        uploadButton.addActionListener(controller.getUploadListener());
        exitItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        aboutItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(aboutMenu, "It's a magic program!\n" +
                        " It helps you to upload your photos from html documents to picasaweb service. =)");
            }
        });

    }

    private void createAndShowGUI() {

        JFrame.setDefaultLookAndFeelDecorated(true);

        frame = new JFrame();
        frame.setPreferredSize(new Dimension(300, 350));
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        addComponents(frame.getContentPane());
        frame.pack();
        frame.setVisible(true);
    }

    public void DemoRun() {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }

    public void showFiles(String filename) {
        File input = new File(filename);
        Scanner scan = null;
        try {
            scan = new Scanner(input);
            StringBuffer sb = new StringBuffer();
            while (scan.hasNextLine()) {
                sb.append(scan.nextLine()).append("  \n");
            }
            log.setText(sb.toString());
            log.repaint();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            scan.close();
        }

    }

    public JFrame getFrame() {
        return frame;
    }

    public void setUploadDialog(UploadDialog uploadDialog) {
        this.uploadDialog = uploadDialog;
    }

    


}

